package com.jie.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.commonutils.ResultEntity;
import com.jie.eduservice.entity.EduCourse;
import com.jie.eduservice.entity.EduTeacher;
import com.jie.eduservice.service.EduCourseService;
import com.jie.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 首页显示的Controller类
 * @author XieZhiJie
 * @create 2020-10-14 22:08
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    private final EduCourseService courseService;

    private final EduTeacherService teacherService;

    @Autowired
    public IndexFrontController(
            EduCourseService courseService,
            EduTeacherService teacherService
    ) {
        this.courseService = courseService;
        this.teacherService = teacherService;
    }

    // 查询前8条热门课程, 查询前4条名师
    @GetMapping("/index")
    public ResultEntity index() {
        // 查询前8热门课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("id");
        courseQueryWrapper.last("LIMIT 8");
        List<EduCourse> courseList = courseService.list(courseQueryWrapper);

        // 查询前4条名师
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("id");
        teacherQueryWrapper.last("LIMIT 4");
        List<EduTeacher> teacherList = teacherService.list(teacherQueryWrapper);

        return ResultEntity.ok().data("eduList", courseList).data("teacherList", teacherList);
    }

}
